package dm.ast.model.gen;

import dm.ast.DmASTNode;
import dm.ast.DmSelect;
import dm.ast.DmWith;
import dm.ast.model.gen.factory.ComplexityModel;
import dm.ast.model.gen.factory.WithModelFactory;

/**
 * @author weihuixin
 * @date 2024/2/29
 * @Description
 */
public class WithGen extends ASTGenBase {
    @Override
    public DmASTNode gen(int astLevel,int wid_t,int dep_t){
        DmWith dmWith = new DmWith();
        dmWith.setTargetWidt(wid_t);
        dmWith.setTargetDept(dep_t);
        dmWith.setASTLevel(astLevel);

        ComplexityModel model = WithModelFactory.withModels(wid_t,dep_t);  //model count comps[ctes] ASTComplex
        for(int i = 0;i<model.count;i++){
            SelectGen selGen = new SelectGen();
            if(model.comps[i].wid_t > 0){
                //根据宽度,深度生成查询语句作为CTE
                DmSelect select = (DmSelect) selGen.gen(astLevel,model.comps[i].wid_t,model.comps[i].dep_t);
                dmWith.addCTE(select);
            }
        }
        return dmWith;
    }
}
